package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.RedEnvelopeListDetails;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.FormatUtil;
import com.bq.gametrade.utils.ToastUtils;
import com.bq.gametrade.view.CircleImageView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RedEnvelopeDetailsActivity extends BaseActivity implements View.OnClickListener{

    private LinearLayout layBack;
    private TextView tvTitle;
    private ArrayList<RedEnvelopeListDetails> mList = new ArrayList<>() ;
    private int position;
    private TextView tvName,tvMoneyCount,tvMyRedEnvelopeCuont;
    private ImageView ivTicket,ivFriendHeadImage;
    private CircleImageView headImage;
    private LinearLayout layFriend,layMy;
    private LinearLayout layNoReceive,layReceived,layTicket;
    private TextView tvFriendReceiveMoney,tvSendRedEnvelopeMoney;

    private LinearLayout layFriendReceiveTicket;
    private TextView tvFriendReceiveTicket;
    private  LinearLayout laySendRedEnvelopeTicket;
    private TextView tvSendRedEnvelopeTicket,tvReceiveUser;
    private UserInfoDetails userInfo;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.color_d84e43));
        }
        setContentView(R.layout.activity_redenvelope_details);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        mList = getIntent().getParcelableArrayListExtra("redEnvelopeList");
        if(mList.size()<0){
           finish();
        }
        position = getIntent().getIntExtra("position",0);
        initView();
        //打开好友发给自己的红包
        if((mList.get(position).revice_user.equals(application.getUserInfo().account))){

            layMy.setVisibility(View.GONE);
            layFriend.setVisibility(View.VISIBLE);
            if(mList.get(position).m_status.equals("0")){
                toReceiveRedEnvelope(mList,position);
            }else if(mList.get(position).m_status.equals("1")) {
                setViewFriendData(mList,position);

            }
            //打开自己发给好友的红包
        }else {
            layFriend.setVisibility(View.GONE);
            layMy.setVisibility(View.VISIBLE);
            setViewMyData(mList,position);
        }





    }

    private void initView() {
        layBack = serchViewById(R.id.lay_back);
        layBack.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("红包详情");
        tvName = serchViewById(R.id.tv_Name);
        tvMoneyCount = serchViewById(R.id.tv_moneyCount);
        ivTicket = serchViewById(R.id.iv_ticket);
        headImage = serchViewById(R.id.user_headimage);
        layFriend = serchViewById(R.id.lay_friend);
        layMy = serchViewById(R.id.lay_my);
        layTicket = serchViewById(R.id.lay_ticket);
        layNoReceive = serchViewById(R.id.lay_noReceive);
        layReceived = serchViewById(R.id.lay_received);
        tvMyRedEnvelopeCuont = serchViewById(R.id.tv_myRedEnvelopeCuont);
        tvFriendReceiveMoney = serchViewById(R.id.tv_friendReceiveMoney);
        tvSendRedEnvelopeMoney = serchViewById(R.id.tv_sendRedEnvelopeMoney);

        layFriendReceiveTicket = serchViewById(R.id.lay_friendReceiveTicket);
        tvFriendReceiveTicket = serchViewById(R.id.tv_friendReceiveTicket);

        laySendRedEnvelopeTicket = serchViewById(R.id.lay_sendRedEnvelopeTicket);
        tvSendRedEnvelopeTicket = serchViewById(R.id.tv_sendRedEnvelopeTicket);

        tvReceiveUser = serchViewById(R.id.tv_receiveUser);
        ivFriendHeadImage = serchViewById(R.id.my_friendHeadImage);


    }

    private void setViewFriendData(ArrayList<RedEnvelopeListDetails> datas,int mPosition){
        if(TextUtils.isEmpty(userInfo.nickname)){
            tvName.setText(userInfo.account+"的红包");
        }else {
            tvName.setText(userInfo.nickname+"的红包");
        }
        Glide.with(RedEnvelopeDetailsActivity.this)
                .load(datas.get(mPosition).send_img)
                .error( R.mipmap.user_headimg_gray )
                .into(headImage);
        if(datas.get(position).type.equals("0")){
            ivTicket.setVisibility(View.GONE);
            tvMoneyCount.setText(FormatUtil.valueFormatWithTwo(datas.get(position).num)+"元");
        }else {
            ivTicket.setVisibility(View.VISIBLE);
            tvMoneyCount.setText(datas.get(position).num);
        }
    }
    private void setViewMyData(ArrayList<RedEnvelopeListDetails> datas,int mPosition){

        if(datas.get(mPosition).m_status.equals("0")){
            layNoReceive.setVisibility(View.VISIBLE);
            layReceived.setVisibility(View.GONE);
            Glide.with(RedEnvelopeDetailsActivity.this)
                    .load(datas.get(position).send_img)
                    .error( R.mipmap.user_headimg_gray )
                    .into(headImage);
            tvName.setVisibility(View.VISIBLE);
            if(TextUtils.isEmpty(userInfo.nickname)){
                tvName.setText(userInfo.account);
            }else {
                tvName.setText(userInfo.nickname);
            }

            if(datas.get(mPosition).type.equals("0")){
                layTicket.setVisibility(View.GONE);
                tvMyRedEnvelopeCuont.setVisibility(View.VISIBLE);
                tvMyRedEnvelopeCuont.setText("红包金额"+FormatUtil.valueFormatWithTwo(datas.get(position).num)+"元，等待对方领取");
            }else if(datas.get(mPosition).type.equals("1")){
                tvMyRedEnvelopeCuont.setVisibility(View.GONE);
                layTicket.setVisibility(View.VISIBLE);
                tvMyRedEnvelopeCuont.setText("红包金额"+datas.get(position).num);

            }
        }else if(datas.get(mPosition).m_status.equals("1")){
            tvName.setText(Base64Util.decodeStr(datas.get(mPosition).send_uname)+"的红包");
            layNoReceive.setVisibility(View.GONE);
            layReceived.setVisibility(View.VISIBLE);
            tvReceiveUser.setText(Base64Util.decodeStr(mList.get(mPosition).revice_uname));
            Glide.with(RedEnvelopeDetailsActivity.this)
                    .load(datas.get(position).revice_img)
                    .error( R.mipmap.user_headimg_gray )
                    .into(ivFriendHeadImage);
            Glide.with(RedEnvelopeDetailsActivity.this)
                    .load(datas.get(position).send_img)
                    .error( R.mipmap.user_headimg_gray )
                    .into(headImage);

            if(datas.get(mPosition).type.equals("0")){
                layFriendReceiveTicket.setVisibility(View.GONE);
                tvFriendReceiveMoney.setVisibility(View.VISIBLE);
                tvSendRedEnvelopeMoney.setVisibility(View.VISIBLE);
                laySendRedEnvelopeTicket.setVisibility(View.GONE);
                tvFriendReceiveMoney.setText("1个红包共"+datas.get(mPosition).num+"元");
                tvSendRedEnvelopeMoney.setText(FormatUtil.valueFormatWithTwo(datas.get(mPosition).num)+"元");

            }else {
                layFriendReceiveTicket.setVisibility(View.VISIBLE);
                tvFriendReceiveMoney.setVisibility(View.GONE);
                tvSendRedEnvelopeMoney.setVisibility(View.GONE);
                laySendRedEnvelopeTicket.setVisibility(View.VISIBLE);
                tvFriendReceiveTicket.setText("1个红包共"+datas.get(mPosition).num);
                tvSendRedEnvelopeTicket.setText(datas.get(mPosition).num);

            }
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_back:
                finish();
                break;

        }
    }

    private void toReceiveRedEnvelope(final ArrayList<RedEnvelopeListDetails> list, final int positions){
        Map<String,String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("luck_id",list.get(positions).id);
        showSpotsDialog("正在打开····");
        VRHttp.sendRequest(RedEnvelopeDetailsActivity.this, HttpLink.RECEVIER_REDENVELOPE_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if(response != null){
                    ResponseBean responseBean = (ResponseBean)response;
                    if(responseBean.status.equals("0")) {
                        toastShow("领取成功");
                        setViewFriendData(list, positions);
                    }else if (responseBean.status.equals("99")) {
                            ToastUtils.showToast(RedEnvelopeDetailsActivity.this ,"登录超时");
                            application.setUserInfo(null);
                            startActivity(new Intent(RedEnvelopeDetailsActivity.this, LoginActivity.class));
                        } else if (responseBean.status.equals("98")) {
                            ToastUtils.showToast(RedEnvelopeDetailsActivity.this, "您的账号已在其它设备登录，请重新登录修改您的密码");
                            application.setUserInfo(null);
                            startActivity(new Intent(RedEnvelopeDetailsActivity.this, LoginActivity.class));
                        }
                }

            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
